<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Popup extends CH_Controller{

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	
	 /**
        * Constructor
     */
	function __construct() { 
		parent::__construct(); 
		$this->init();
		$this->load->helper(array('form','array', 'url','common','login'));
		$this->load->model('popup_model');
    } 
    	
	/*
	 @팝업관리
	*/
	public function popupList(){
		$url = $this->uri->segment(1).'/'.$this->uri->segment(2);
		$uri_array = segment_explode($this->uri->uri_string());		
		$array_segment = array_keys($this->uri->segment_array(), "page");
		$page = in_array('page', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'page'))) : 1;
		$uri_segment = in_array('page', $uri_array) ? $array_segment[0]+1 : 0;
		$keyname = urldecode($this->security->xss_clean(url_explode($uri_array, 'keyname')));
		$keyword = urldecode($this->security->xss_clean(url_explode($uri_array, 'keyword')));
		$param['keyname'] = $keyname;
		$param['keyword'] = $keyword;
		$param['page'] = $page;
		
		$listnum = $this->config->item('ch_listnum');		
		
		$count = $this->popup_model->popup_list_total($param);
		$result = $this->popup_model->popup_list($param);
		
		$this->load->library('pagination');
		$config['base_url'] = '/popup/page/';
		$config['total_rows'] = $count;
		$config['per_page'] = $listnum;
		$config['use_page_numbers'] = true;
		
		$config['uri_segment'] = $uri_segment;			
		$this->pagination->initialize($config);

		
		$list_no = $count - $listnum * ($page-1);
		$data = array(
				'_title' => '팝업관리',
				'_group' => 'homepage',
				'_content' => $this->config->item('ch_manager_root').'/'.$url,
				'rst' => $result,
				'paging'=> $this->pagination->create_links(),
				'list_no'=> $list_no,
				'page'=>$page 
		);
		
		return $data;
	}
	
	function popupForm(){		
		$uri_array = segment_explode($this->uri->uri_string());
		$page = in_array('page', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'page'))) : 1;
		$popup_id = in_array('id', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'id'))) : 1;
		$url = $this->uri->segment(1).'/'.$this->uri->segment(2);
		
		$ckeditor = array('path'=> 'asset/ckeditor','id'=>'textarea_id','config'=>array('filebrowserImageUploadUrl'=>'/asset/ckeditor/upload.php?type=Images'));
		
		$data = array(
				'_title' => '팝업관리',
				'_group' => 'homepage',
				'_content' => $this->config->item('ch_manager_root').'/'.$url,
				'_ckeditor' => $ckeditor
		);
		
		$data = array_merge($data, $this->popup_model->select($popup_id));
		return $data;
	}
	
	function popupDbjob(){
		$data = null;
		$msg = '';
		$this->load->library('form_validation');
		$dbjob = $this->input->post('dbjob');
		$this->form_validation->set_rules('title', '제목', 'required');
		if ($this->form_validation->run() == FALSE){
			$this->alert->jalert('잘못된 접근 입니다.', '','back');
		}else{
			switch($dbjob){
				case('i'):
					$msg = '저장되었습니다.';
					$data = array(
							'title' => $this->input->post('title') ,
							'start_date' => $this->input->post('start_date') ,
							'end_date' => $this->input->post('end_date') ,
							'layer_yn' => $this->input->post('layer_yn') ,
							'open_yn' => $this->input->post('open_yn') ,
							'popup_style' => $this->input->post('popup_style') ,
							'popup_template' => $this->input->post('popup_template') ,
							'popup_content' => $this->input->post('content') ,
							'popup_top' => $this->input->post('popup_top') ,
							'popup_left' => $this->input->post('popup_left') ,
							'popup_height' => $this->input->post('popup_height') ,
							'popup_width' => $this->input->post('popup_width') ,
							'reg_date' => date("Y-m-d H:i",time())
					);
				break;
				case('u'):
					$msg = '수정되었습니다.';
					$data = array(
							'title' => $this->input->post('title') ,
							'start_date' => $this->input->post('start_date') ,
							'end_date' => $this->input->post('end_date') ,
							'layer_yn' => $this->input->post('layer_yn') ,
							'open_yn' => $this->input->post('open_yn') ,
							'popup_style' => $this->input->post('popup_style') ,
							'popup_template' => $this->input->post('popup_template') ,
							'popup_content' => $this->input->post('content') ,
							'popup_top' => $this->input->post('popup_top') ,
							'popup_left' => $this->input->post('popup_left') ,
							'popup_height' => $this->input->post('popup_height') ,
							'popup_width' => $this->input->post('popup_width')
					);
				break;
				case('d'):
					$msg = '삭제 되었습니다.';
				break;
			}
			
			
			if(!empty($_FILES['file1']['tmp_name'])){
				$this->load->library('Upload',array('allow_file'=>null,'allow_size'=> null,'file'=>'file1','upload_path'=>'popup'));
				$this->Upload->fileUpload();
				$file = $this->upload->getUploadResult();
				$data = array_merge($data, array('popup_image'=> $file['real_file']));
			}
			
			$this->popup_model->dbjob($dbjob,$data);
			$this->alert->jalert($msg, base_url('/popup'));
		}
		exit;
	}
}

/* End of file html.php */
/* Location: ./application/controllers/member.php */